<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Banner;
use app\common\model\Contact;
use app\common\model\Article;
use app\common\model\CaseModel;
use app\common\model\Cate;
use app\common\model\Hot;
use app\common\model\Search;
use function Couchbase\defaultDecoder;
use think\facade\Cookie;

class Index extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $localserch = get_cookie_history();
        $contact = Contact::find(1);
        if ($contact) {
            $contact['code'] = env('app.yuming') . $contact['code'];
            $contact['logo'] = env('app.yuming') . $contact['logo'];
        }
        //搜索记录
        $search = Search::order('num desc')->field('title,res_num')->limit(0, 8)->select();
        //热门话题
        $hot = Hot::where('status', 1)
            ->field('id,title,image')
            ->limit(0, 8)
            ->order('id asc')
            ->select();
        if ($hot) {
            $hot = changeImage($hot, 'image');
            //$case = changetime($case , 'created_at' );
            // $hot = delHtml($hot, 'content');
        }
        //优秀案例
        $anlicate = Cate::where(['type' => 1, 'status' => 1])->select()->toArray();
        $anlicate = array_column($anlicate, 'id');
        $anli = CaseModel::where('cate_id', 'in', $anlicate)
            ->where('status', 1)
            ->field('id,title,image')
            ->order('id asc')
            ->limit(0, 3)
            ->select();
        if ($anli) {
            $anli = changeImage($anli, 'image');
            //$case = changetime($case , 'created_at' );
            // $anli = delHtml($anli, 'content');
        }
        //素材模板
        $sucaicate = Cate::where(['type' => 2, 'status' => 1])->select()->toArray();
        $sucaicate = array_column($sucaicate, 'id');
        $sucai = CaseModel::where('cate_id', 'in', $sucaicate)
            ->where('status', 1)
            ->field('id,title,image,file_path')
            ->order('id asc')
            ->limit(0, 8)
            ->select();

        if ($sucai) {
            $sucai = changeImage($sucai, 'image');
            $sucai = changeImage($sucai, 'file_path');
            //$case = changetime($case , 'created_at' );
            //$sucai = delHtml($sucai, 'content');
        }
        //代码知识
        $daimacate = Cate::where(['type' => 3, 'status' => 1])->select()->toArray();
        $daimacate = array_column($daimacate, 'id');
        $daima = Article::where('cate_id', 'in', $daimacate)
            ->where('status', 1)
            ->field('id,title,image')
            ->order('id asc')
            ->limit(0, 7)
            ->select();
        if ($daima) {
            $daima = changeImage($daima, 'image');
            //$case = changetime($case , 'created_at' );
            //  $daima = delHtml($daima, 'content');
        }
        //技术社区
        $shequcate = Cate::where(['type' => 5, 'status' => 1])->select()->toArray();
        $shequcate = array_column($shequcate, 'id');
        $shequ = Article::where('cate_id', 'in', $shequcate)
            ->where('status', 1)
            ->with('getmember')
            ->field('id,title,image,member_id,created_at')
            ->order('id asc')
            ->limit(0, 4)
            ->select();
        if ($shequ) {
            $shequ = changeImage($shequ, 'image');
            //$case = changetime($case , 'created_at' );
            //$shequ = delHtml($shequ, 'content');
        }

        $data = [
            'contact' => $contact,
            'localsearch' => $localserch,
            'scarch' => $search,
            'hot' => $hot,
            'anli' => $anli,
            'sucai' => $sucai,
            'daima' => $daima,
            'shequ' => $shequ,
        ];
        return sucessMsg(200, '请求成功', $data);
    }

    public function search()
    {
        $keyword = input('keyword');
        if (!$keyword) {
            return errMsg(400, '请输入关键词');
        }
        set_cookie_history($keyword);
        $case = CaseModel::where('title', 'like', "%$keyword%")
            ->field('id,title,image,file_path,content,created_at')
            ->where('status', 1)
            ->select();

        $anli_cate = Cate::where(['type' => 1, 'status' => 1])->select()->toArray();
        $anli_cate = array_column($anli_cate, 'id');
        $sucai_cate = Cate::where(['type' => 2, 'status' => 1])->select()->toArray();
        $sucai_cate = array_column($sucai_cate, 'id');
        $sucai = [];
        $anli = [];
        if ($case) {
            $case = changeImage($case, 'image');
            $case = changeImage($case, 'file_path');

            //$case = changetime($case , 'created_at' );
            $case = delHtml($case, 'content');
            foreach ($case as $v) {
                if (in_array($v['cate_id'], $anli_cate)) {
                    $anli[] = $v;
                } else {
                    $sucai[] = $v;
                }
            }
        }
        $article = Article::where('title', 'like', "%$keyword%")
            ->where('status', 1)
            ->select();
        $daima_cate = Cate::where(['type' => 3, 'status' => 1])->select()->toArray();
        $daima_cate = array_column($daima_cate, 'id');
        $wenda_cate = Cate::where(['type' => 4, 'status' => 1])->select()->toArray();
        $wenda_cate = array_column($wenda_cate, 'id');
        $shequ_cate = Cate::where(['type' => 5, 'status' => 1])->select()->toArray();
        $shequ_cate = array_column($shequ_cate, 'id');

        $daima = [];
        $wenda = [];
        $shequ = [];
        if ($article) {
            $article = changeImage($article, 'image');
            //$case = changetime($case , 'created_at' );
            $article = delHtml($article, 'content');
            foreach ($article as $v) {
                if (in_array($v['cate_id'], $daima_cate)) {
                    $daima[] = $v;
                } elseif (in_array($v['cate_id'], $wenda_cate)) {
                    $wenda[] = $v;
                } else {
                    $shequ[] = $v;
                }
            }
        }


        $hot = Hot::where('title', 'like', "%$keyword%")->where('status', 1)->select();
        if ($hot) {
            $hot = changeImage($hot, 'image');
            //$case = changetime($case , 'created_at' );
            $hot = delHtml($hot, 'content');
        }
        $count = count($case) + count($article) + count($hot);

        $search = Search::where('title', $keyword)->find();
        if (!$search) {
            $data = ['title' => $keyword, 'num' => 1, 'created_at' => time(), 'res_num' => $count, 'updated_at' => time()];
            $add = Search::insert($data);
        } else {
            $data = ['num' => $search['num'] + 1, 'updated_at' => time(), 'res_num' => $count];
            $edit = Search::where('id', $search['id'])->update($data);
        }
        $res = ['count' => $count, 'anli' => $anli, 'sucai' => $sucai, 'daima' => $daima, 'wenda' => $wenda, 'shequ' => $shequ];
        return sucessMsg(200, '获取成功', $res);
    }

    public function banner()
    {
        $type = input('type/d', 1);
        $banner = Banner::where(['status' => 1, 'type' => $type])->field('id,title,image')->find();
        $banner['image'] = env('database.domin') . '/' . $banner['image'];
        return sucessMsg(200, '获取成功！', $banner);
    }
}
